package com.xd.study.activity;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.util.Log;
import android.widget.DatePicker;
import android.widget.TimePicker;

import com.xd.study.R;

import java.util.Calendar;

/**
 * Created by DGF on 2015/4/22.
 * DatePicker
 * TimePicker
 * DatePickerDialog
 * TimePickerDialog
 */
public class DatePickerActivity extends Activity {

    private int year;
    private int month;
    private int day;
    private int hour;
    private int minute;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.datepicker);

        //日历，用于获取当前时间
        Calendar cal = Calendar.getInstance();
        year = cal.get(Calendar.YEAR);
        month = cal.get(Calendar.MONTH)+1;
        day = cal.get(Calendar.DAY_OF_MONTH);
        hour = cal.get(Calendar.HOUR_OF_DAY);
        minute = cal.get(Calendar.MINUTE);

        setTitle(year +"-"+ month +"-"+ day +" "+ hour +":"+ minute);

        DatePicker datePicker = (DatePicker)this.findViewById(R.id.datePicker);
        datePicker.init(year, month -1, day, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                DatePickerActivity.this.year = year;
                month = monthOfYear + 1;
                day = dayOfMonth;
                setTitle(year +"-"+ month +"-"+ day +" "+ hour +":"+ minute);
            }
        });

        TimePicker timePicker = (TimePicker)this.findViewById(R.id.timePicker);
        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                hour = hourOfDay;
                DatePickerActivity.this.minute = minute;
                setTitle(year +"-"+ month +"-"+ day +" "+ hour +":"+ minute);
            }
        });

        new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Log.i("Tag", year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
            }
        }, year, month-1, day).show();

        new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                Log.i("Tag", hourOfDay+":"+minute);
            }
        }, hour, minute, true).show();
    }
}
